package dao;


import java.util.Date;
import java.util.List;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import beans.User;

import beans.Comment;
import beans.Restaurant;
@Transactional
public class CommentDAO {

	private HibernateTemplate hibernateTemplate;
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	public void saveComment(Comment comment){
		comment.setDate(new Date());
		this.hibernateTemplate.save(comment);
	}
	public List<Comment> GetAllComments(){
		return this.hibernateTemplate.loadAll(Comment.class);
	}
	public List<Comment> GetAllCommentsFromRestaurant(Restaurant restaurant){
		
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		List list =  session.createQuery("from Comment where restaurant_id=:id order by date desc").setInteger("id",restaurant.getId()).list();
		return list;
	}
	public List<Comment> GetAllUserComments(User user){
		
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List list =  session.createQuery("from Comment where username=:username order by date desc").setString("username",user.getUsername()).list();
		return list;
	}
}
